package com.it135.citytraffic.exceptionHandler;


import com.it135.citytraffic.exception.CustomException;
import com.it135.citytraffic.pojo.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result<String> sqlExceptionHandler(Exception e) {
        if (e.getMessage().contains("Duplicate entry")) {
            String[] s = e.getMessage().split(" ");
            return Result.error(s[9] + "已存在！");
        }
        return Result.error("操作失败！");
    }

    @ExceptionHandler(CustomException.class)
    public Result<String> customExceptionHandler(Exception e) {
        return Result.error(e.getMessage());
    }
}
